﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Telerik.Web.UI;
using SMA.GENTEv1;

public partial class Protegidos_Administracao_Cadastros_acCadDadosGestor : System.Web.UI.Page
{
    protected int idGestor = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["idGestor"] != null)
            idGestor = Int32.Parse(Request.QueryString["idGestor"].ToString());

        if (idGestor != 0)
        {
            CarregarDadosGestor();
            CarregarDadosContrato();
        }
        else
        {
            Response.Redirect("acDadosGestor.aspx");
        }
    }

    #region Eventos

    protected void RadAjaxManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
    {
        if (e.Argument == "RebindContrato")
        {
            RadGrid2.MasterTableView.SortExpressions.Clear();
            RadGrid2.MasterTableView.GroupByExpressions.Clear();
            RadGrid2.MasterTableView.CurrentPageIndex = RadGrid2.MasterTableView.PageCount - 1;
            RadGrid2.Rebind();
            CarregarDadosContrato();
        }
    }

    protected void RadGrid2_DeleteCommand(object source, GridCommandEventArgs e)
    {
        int idContrato = int.Parse((e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["IdContrato"].ToString());

        Pessoa pessoa = new Pessoa();
        pessoa.IdPessoa = idGestor;

        Contrato contrato = new Contrato();
        contrato.IdContrato = idContrato;

        ContratoGestor contratoGestor = new ContratoGestor();
        //contratoGestor.IdContratoGestor = idContratoGestor;
        contratoGestor.Pessoa = pessoa;
        contratoGestor.Contrato = contrato;
        contratoGestor.Excluir();

        CarregarDadosContrato();
    }
    #endregion

    #region Metodos
    private void CarregarDadosGestor()
    {
        Pessoa gestor = new Pessoa(idGestor);

        lblNome.Text = gestor.Nome;
        lblMatricula.Text = gestor.Matricula;
        hdId.Value = gestor.IdPessoa.ToString();
    }
    private void CarregarDadosContrato()
    {
        ContratoGestor contratoGestor = new ContratoGestor();
        List<ContratoGestor> listContratoGestor = contratoGestor.SelectPorGestor(idGestor);
        
        List<Contrato> listContrato = new List<Contrato>();

        foreach (ContratoGestor item in listContratoGestor)
        {
    		 listContrato.Add(item.Contrato);
        }

        RadGrid2.DataSource = listContrato;
        RadGrid2.DataBind();
    }

    #endregion
}
